package com.yhf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhf.bean.Clazz;
import com.yhf.bean.Grade;
import com.yhf.service.ClazzService;
import com.yhf.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clazz")
public class ClazzController {
    private Map<String,Object> result = new HashMap<>();

    @Autowired
    private ClazzService clazzService;
    @Autowired
    private GradeService gradeService;
    @GetMapping("/goClazzListView")
    //年级获取
    public ModelAndView goClazzListView(ModelAndView modelAndView){
        modelAndView.addObject("gradeList",gradeService.selectAll());
        modelAndView.setViewName("clazz/clazzList");
        return modelAndView;
    }

    @PostMapping("/getClazzList")
    @ResponseBody
    public Map<String, Object> getClazzList(Integer page, Integer rows, String clazzname,String gradename) {

        Clazz clazz = new Clazz();
        clazz.setName(clazzname);
        clazz.setGrade_name(gradename);

        //设置每页记录数
        PageHelper.startPage(page, rows);


        //获取指定的和管理员或者是所有的管理员数据
        List<Clazz> clazzList = clazzService.getClazzList(clazz);

        PageInfo<Clazz> clazzPageInfo = new PageInfo<>(clazzList);
        //总页数

        long total = clazzPageInfo.getTotal();
        result.put("total", total);
        result.put("rows", clazzList);
        return result;

    }
    @PostMapping("/addClazz")
    @ResponseBody
    public Map<String, Object> addClazz(Clazz clazz) {
        Clazz clazz1= clazzService.findByName(clazz.getName());
        if (clazz1 == null) {
            if (clazzService.addClazz(clazz) > 0) {
                result.put("success", true);
            } else {
                result.put("success", false);
                result.put("msg", "服务器发生异常，添加失败！");
            }

        } else {
            result.put("success", false);
            result.put("msg", "用户名已存在");
        }
        return result;
    }
    @PostMapping("/editClazz")
    @ResponseBody
    public Map<String, Object> editClazz(Clazz clazz) {
        //需要排除用户名只修改账户名以外信息
        Clazz clazz1 = clazzService.findByName(clazz.getName());
        if (clazz1 != null) {
            if (!(clazz.getId().equals(clazz1.getId()))) {
                result.put("success", false);
                result.put("msg", "该用户名已存在，请更改重试！");
                return result;
            }
        }
//修改操作
        if (clazzService.updateClazz(clazz) > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "服务器异常，修改失败！");
        }
        return result;

    }
    @PostMapping("/deleteClazz")
    @ResponseBody
    public Map<String, Object> deleteClazz(@RequestParam(value = "ids[]", required = true) Integer[] ids) {
        if (clazzService.deleteClazz(ids) > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
            result.put("msg", "服务器异常，删除失败！");
        }
        return result;
    }

}
